package com.kingbull.recorder.adapter;

import com.kingbull.recorder.ResourceTable;
import com.kingbull.recorder.utils.RippleUtil;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * 列表 Adapter
 */
public class TextAdapter extends BaseItemProvider {

    private final List<String> titles;
    private final Context context;

    /**
     * 初始化
     *
     * @param context Context
     * @param titles  数据
     */
    public TextAdapter(Context context, List<String> titles) {
        this.context = context;
        this.titles = titles;
    }

    @Override
    public int getCount() {
        if (titles == null) {
            return 0;
        }
        return titles.size();
    }

    private Context getContext() {
        return context;
    }

    @Override
    public String getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, final ComponentContainer componentContainer) {
        ListViewHolder listViewHolder;
        if (component == null) {
            component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item_main, componentContainer, false);
            listViewHolder = new ListViewHolder();
            listViewHolder.text = (Text) component.findComponentById(ResourceTable.Id_text1);
            component.setTag(listViewHolder);
            RippleUtil.setComponent(component);
        } else {
            listViewHolder = (ListViewHolder) component.getTag();
        }
        listViewHolder.text.setText(getItem(position));
        return component;
    }

    private static class ListViewHolder {
        Text text;
    }

}
